package com.xjnt.frameworks.utils;

import java.nio.ByteBuffer;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;

/**
 * @FileName : UUIdUtil.java
 * @Description :
 * @Copyright : Copyright (c) 2015
 * @author caoyong
 * @date Jul 28, 2015 2:53:04 PM
 */
public class UUIDKit {
	/**
	 * Sun Java 32 UUID
	 * @return
	 *
	 * @author caoyong
	 */
	public static String uuid() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	/**
	 * Base64 22 UUID
	 * @return
	 *
	 * @author caoyong
	 */
	public static String base64Uuid() {
		UUID uuid = UUID.randomUUID();
		return base64Uuid(uuid);
	}

	protected static String base64Uuid(UUID uuid) {

		ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
		bb.putLong(uuid.getMostSignificantBits());
		bb.putLong(uuid.getLeastSignificantBits());

		return Base64.encodeBase64URLSafeString(bb.array());
	}

	public static String encodeBase64Uuid(String uuidString) {
		UUID uuid = UUID.fromString(uuidString);
		return base64Uuid(uuid);
	}

	public static String decodeBase64Uuid(String compressedUuid) {

		byte[] byUuid = Base64.decodeBase64(compressedUuid);

		ByteBuffer bb = ByteBuffer.wrap(byUuid);
		UUID uuid = new UUID(bb.getLong(), bb.getLong());
		return uuid.toString();
	}

	/**
	 * Base58 22 UUID
	 * @return
	 *
	 * @author caoyong
	 */
	public static String base58Uuid() {
		UUID uuid = UUID.randomUUID();
		return base58Uuid(uuid);
	}

	protected static String base58Uuid(UUID uuid) {

		ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
		bb.putLong(uuid.getMostSignificantBits());
		bb.putLong(uuid.getLeastSignificantBits());

		return Base58.encode(bb.array());
	}

	public static String encodeBase58Uuid(String uuidString) {
		UUID uuid = UUID.fromString(uuidString);
		return base58Uuid(uuid);
	}

	public static String decodeBase58Uuid(String base58uuid) {
		byte[] byUuid = Base58.decode(base58uuid);
		ByteBuffer bb = ByteBuffer.wrap(byUuid);
		UUID uuid = new UUID(bb.getLong(), bb.getLong());
		return uuid.toString();
	}
}
